// pages/fabu/fabu.js
const app = getApp();

Page({
  data: {
    picker : ['餐饮', '快递'],
    listtype:'',
    listtitle:'',
    listtext:'',
    getaddress:'',
    toaddress:'',
    username:'',
    userid:'0',
    listcost:'',
    userphone:'',
    listArr: [],
    addressArr:[],
    userId: 1,
    CustomBar: app.globalData.CustomBar,
    checkbox: [{
      value: 0,
      data:{
        coupononenum:app.data.userInfo.coupononenum
      },
      name: '满20减5',
      checked: false,
      hot: false,
    }, {
      value: 1,
      data:{
        coupontwonum:app.data.userInfo.coupontwonum
      },
      name: '满50减10',
      checked: false,
      hot: false,
    }, ]

  },

  
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  }, 
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  choswAddress(e){
    console.log(e);
    this.setData({
      toaddress: e.currentTarget.dataset.addressid
    })
  },

  showModal1(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },  
  hideModal1(e) {
    this.setData({
      modalName: null
    })
  },
  ChooseCheckbox(e) {
    let items = this.data.checkbox;
    let values = e.currentTarget.dataset.value;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == values) {
        items[i].checked = !items[i].checked;
      }
    }
    this.setData({
      checkbox: items
    })
    
  },
  getaddress(){
    var that=this;
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/address/selectaddressbyid',
        method:'POST',
        data:{
          userId : app.data.userInfo.userid
        },
        header:{'content-type':'application/x-www-form-urlencoded'},
      success: function (res) {
        var resData = res.data;
        console.log("数据",resData);
        console.log("this",this);
        that.setData({
          addressArr: resData.data
        })
      }
      }) 
  },
  
  creattask(){
    wx.request({
      url: 'https://www.woodymidnight.cn/BCUHelper/api/list/insertlist',
      method:'POST',
      header:{'content-type':'application/x-www-form-urlencoded'},
      data:{
        listtype: this.data.listtype,
        listtitle: this.data.listtitle,
        listtext: this.data.listtext,
        getaddress: this.data. getaddress,
        toaddress: this.data.toaddress,
        username: app.data.userInfo.username,
        userphone: this.data.userphone,
        userid :app.data.userInfo.userid,
        userimage : app.data.userInfo.userimage,
        listcost: this.data.listcost
      },
      success:(res)=>{
        if(this.data.data == null){
          this.setData({
            listtype : '',
            listtitle : '',
            listtext : '',
            getaddress : '',
            toaddress : '',
            userphone : '',
            listcost : ''
          })
        }
        console.log(res.data);
      
      }
    })
    wx.switchTab({
      url: '/pages/index/index',
    })
  wx.showToast({
    title: '创建成功',
    icon:'success',
    duration:1500
  })
   
      
  },
  showDialogBtn(){
    var that=this;
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/list/insertlist',
        method:'POST',
        data:{
          userid : this.data.userid,
          userphone: app.data.userInfo.userphone,
        },
        header:{'content-type':'application/x-www-form-urlencoded'},
      success: function (res) {
        var resData = res.data;
        console.log("数据",resData);
        console.log("this",this);
        that.setData({
          listArr: resData.data
        })
      }
      }) 
  },
  showDialogBtn: function (e) {
    this.setData({       
      showModal: true,
        userid :app.data.userInfo.userid,
    
    })
     
    },
    preventTouchMove: function () {
       
    },
    hideModal: function () {
       
      this.setData({
       
      showModal: false
       
      });
       
      },
      onCancel: function () {
       
        this.hideModal();
         
        },
         
         
        onConfirm: function () {
        
        wx.showToast({
         
        title: '提交成功',
         
        icon: 'success',
         
        duration: 2000,
         
   
        })
        wx.navigateTo({
          url: '/pages/index/index',
        })
         
        this.hideModal();
         
        },
    
  inlistcost:function(e){
    console.log(e);
    this.setData({
      listcost:e.detail.value,
    })
    if(this.data.checkbox[0].checked == true&&this.data.listcost>=20&&this.data.listcost<50){
      this.setData({
        listcost:e.detail.value-5,
        coupononenum:e.detail.value-1
      })
    }
    if(this.data.checkbox[1].checked == true&&this.data.listcost>=50){
      this.setData({
        listcost:e.detail.value-5,
        coupontwonum:e.detail.value-1
      })
    }
    if(this.data.checkbox[0].checked==true){
      this.data.checkbox[1].checked==false
    }else if(this.data.checkbox[1].checked==true){
      this.data.checkbox[0].checked==false
    }
  },
  inlisttitle:function(e){
    console.log(e);
    this.setData({
      listtitle:e.detail.value,
    })
  },
  inlisttext:function(e){
    console.log(e);
    this.setData({
       listtext:e.detail.value,
    })
  },
  ingetaddress:function(e){
    console.log(e);
    this.setData({
      getaddress:e.detail.value,
    })
  },
  intoaddress:function(e){
    console.log(e);
    this.setData({
      toaddress:e.detail.value,
    })
  },
  inusername:function(e){
    console.log(e);
    this.setData({
      username:e.detail.value,
    })
  },
   inuserphone:function(e){
    console.log(e);
    this.setData({
      userphone:e.detail.value,
    })
  },
  PickerChange(e) {
    console.log(e.detail.value);
    let x = parseInt(e.detail.value)
    this.setData({
      listtype: x
    })
  },
 
      

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log(app.data.userInfo);
      this.getaddress();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})